package org.example.manager.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.enums.PageEnum;
import org.example.enums.ShortLinkStateEnum;
import org.example.manager.GroupCodeMappingManager;
import org.example.mapper.GroupCodeMappingMapper;
import org.example.model.GroupCodeMappingDO;
import org.example.util.CommonUtil;
import org.example.vo.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: 谢超
 * @date: 2023/8/22 10:29
 */
@Component
@Slf4j
public class GroupCodeMappingManagerImpl<V> implements GroupCodeMappingManager {

    @Resource
    private GroupCodeMappingMapper groupCodeMappingMapper;

    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {
        GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper
                .selectOne(Wrappers.lambdaQuery(GroupCodeMappingDO.class)
                        .eq(GroupCodeMappingDO::getId, mappingId)
                        .eq(GroupCodeMappingDO::getAccountNo, accountNo)
                        .eq(GroupCodeMappingDO::getGroupId, groupId)
                        .eq(GroupCodeMappingDO::getDel, 0));
        return groupCodeMappingDO;
    }

    @Override
    public int addGroupMapping(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.insert(groupCodeMappingDO);
    }

    @Override
    public int update(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null, Wrappers.lambdaUpdate(GroupCodeMappingDO.class)
                .eq(GroupCodeMappingDO::getId, groupCodeMappingDO.getId())
                .eq(GroupCodeMappingDO::getDel, 0)
                .eq(GroupCodeMappingDO::getAccountNo, groupCodeMappingDO.getAccountNo())
                .eq(GroupCodeMappingDO::getGroupId, groupCodeMappingDO.getGroupId())
                .set(GroupCodeMappingDO::getTitle, groupCodeMappingDO.getTitle())
                .set(GroupCodeMappingDO::getDomain, groupCodeMappingDO.getDomain())
        );
    }

    @Override
    public int delGroupMapping(GroupCodeMappingDO groupCodeMappingDO) {
        // 不传入整个对象，只修改单独的属性
        return groupCodeMappingMapper.update(null, Wrappers.lambdaUpdate(GroupCodeMappingDO.class)
                .eq(GroupCodeMappingDO::getId, groupCodeMappingDO.getId())
                .eq(GroupCodeMappingDO::getAccountNo, groupCodeMappingDO.getAccountNo())
                .eq(GroupCodeMappingDO::getGroupId, groupCodeMappingDO.getGroupId())
                // 单独修改某个属性方式
                .set(GroupCodeMappingDO::getDel, 1));
    }

    /**
     * 分页我们采用了mybatisPlus的分页Page功能
     * Page理论上本身就是符合JSON对象的结构，下面我们使用HashMap返给前端是练习k-v的转换
     * Page本身有个convert属性，里面可以对两个对象进行流式的转换，我们手动实现一下
     * pageData.convert(obj -> beanClone(obj, new GroupCodeMappingVO()));
     */
    @Override
    public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {
        Page<GroupCodeMappingDO> pageInfo = new Page<>(page, size);
        Page<GroupCodeMappingDO> pageData = groupCodeMappingMapper.selectPage(pageInfo, Wrappers.lambdaQuery(GroupCodeMappingDO.class)
                .eq(GroupCodeMappingDO::getAccountNo, accountNo)
                .eq(GroupCodeMappingDO::getGroupId, groupId)
                .eq(GroupCodeMappingDO::getDel, 0));

        HashMap<String, Object> pageMap = new HashMap<>(3);
        pageMap.put(PageEnum.TOTAL_RECORD.getPageMsg(), pageData.getTotal());
        pageMap.put(PageEnum.TOTAL_PAGE.getPageMsg(), pageData.getPages());
        pageMap.put(PageEnum.CURRENT_DATA.getPageMsg(), pageData.getRecords().stream().map(obj -> CommonUtil.beanClone(obj, new GroupCodeMappingVO())).collect(Collectors.toList()));

        return pageMap;
    }

    @Override
    public int updateGroupCodeMappingState(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        return groupCodeMappingMapper.update(null, Wrappers.lambdaUpdate(GroupCodeMappingDO.class)
                .eq(GroupCodeMappingDO::getAccountNo, accountNo)
                .eq(GroupCodeMappingDO::getGroupId, groupId)
                .eq(GroupCodeMappingDO::getCode, shortLinkCode)
                .eq(GroupCodeMappingDO::getDel, 0)
                .set(GroupCodeMappingDO::getState, shortLinkStateEnum.name()));
    }

    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long groupId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(Wrappers.lambdaQuery(GroupCodeMappingDO.class)
                .eq(GroupCodeMappingDO::getCode, shortLinkCode)
                .eq(GroupCodeMappingDO::getAccountNo,accountNo)
                .eq(GroupCodeMappingDO::getGroupId, groupId)
                .eq(GroupCodeMappingDO::getDel, 0));

    }

    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
        GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO, groupCodeMappingVO);
        return groupCodeMappingVO;
    }
}

